package com.android.boncen.commonFrame.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.adapter.GluciseAdapter;
import com.android.boncen.commonFrame.bean.GlucoseHistoryBean;
import com.android.boncen.commonFrame.bean.UserBean;
import com.android.boncen.commonFrame.fragment.AddBMIDialog;
import com.android.boncen.commonFrame.fragment.AddXTDBialog;
import com.android.boncen.commonFrame.fragment.AddXYDialog;
import com.android.boncen.commonFrame.tools.DesityUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * 功能描述: 0血糖-1血压-2血糖蛋白-3BMI  ok
 *
 * @date 2017/2/9
 */

public class PressureActivity extends FragmentActivity {


    public static void start(Context context, int type) {
        // 0血糖-1血压-2血糖蛋白-3BMI
        Intent intent = new Intent(context, PressureActivity.class);
        intent.putExtra("TYPE", type);
        context.startActivity(intent);
    }

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lvList)
    ListView lvList;
    @BindView(R.id.user)
    ImageView user;
    private int page = 1;
    //    0血糖-1血压-2血糖蛋白-3BMI
    private int typeBlood = 1;

    private GluciseAdapter adapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ppb);
        ButterKnife.bind(this);
        user.setImageResource(R.drawable.add_logo_selector);
        lvList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        page++;
                        getGlucoseHistoryBean(page);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        adapter = new GluciseAdapter(this);
        lvList.setAdapter(adapter);

        typeBlood = getIntent().getIntExtra("TYPE", 1);
        tvTitle.setText(typeBlood == 1 ? "血压" : typeBlood == 2 ? "血糖蛋白" : "BMI");
        getGlucoseHistoryBean(page);
    }

    @OnClick({R.id.ib_back, R.id.user})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.user:
                switch (typeBlood) {
                    case 1:
                        // 血压
                        new AddXYDialog(this, new AddXYDialog.SaveCallback() {
                            @Override
                            public void save(float gy, float dy) {
                                addGlucoseHistoryBean(1, gy, dy);
                            }
                        }).show();
                        break;
                    case 2:
                        new AddXTDBialog(this, new AddXTDBialog.SaveCallback() {
                            @Override
                            public void save(float gy) {
                                addGlucoseHistoryBean(2, gy, 0.0f);
                            }
                        }).show();
                        break;
                    case 3:
                        new AddBMIDialog(this, new AddBMIDialog.SaveCallback() {
                            @Override
                            public void save(float gy) {
                                addGlucoseHistoryBean(3, gy, 0.0f);
                            }
                        }).show();
                        break;
                }
                break;
        }
    }

    private void addGlucoseHistoryBean(int type, float bloodSugar, float other) {
        UserBean user = BmobUser.getCurrentUser(UserBean.class);
        GlucoseHistoryBean gameScore = new GlucoseHistoryBean();
        gameScore.setUserId(user.getObjectId());
        gameScore.setTypeBlood(type);
        gameScore.setBloodSugar(bloodSugar);
        gameScore.setOther(other);
        gameScore.save(new SaveListener<String>() {

            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(PressureActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
                    page = 1;
                    getGlucoseHistoryBean(page);
                } else {
                    Toast.makeText(PressureActivity.this, "保存数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void getGlucoseHistoryBean(final int page) {
        BmobUser user = BmobUser.getCurrentUser();
        BmobQuery<GlucoseHistoryBean> query = new BmobQuery<GlucoseHistoryBean>();
        query.addWhereEqualTo("userId", user.getObjectId());    // 查询当前用户的所有
        query.addWhereEqualTo("typeBlood", typeBlood);
        query.order("-updatedAt");
        query.setSkip(20 * (page - 1));
        query.setLimit(20);
        query.findObjects(new FindListener<GlucoseHistoryBean>() {

            @Override
            public void done(List<GlucoseHistoryBean> object, BmobException e) {
                if (e == null) {
                    if (page==1)
                    {
                        adapter.clear();
                    }
                    adapter.setData(object);
                } else {

                }
            }

        });
    }
}
